const { contextBridge, ipcRenderer } = require('electron')

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 截图相关
  takeScreenshot: () => ipcRenderer.invoke('take-screenshot'),
  closeScreenshot: () => ipcRenderer.invoke('close-screenshot'),
  getScreenSources: () => ipcRenderer.invoke('get-screen-sources'),
  saveScreenshot: (dataUrl: string, filename: string) => 
    ipcRenderer.invoke('save-screenshot', dataUrl, filename),
  autoSaveScreenshot: (dataUrl: string, filename: string) => 
    ipcRenderer.invoke('auto-save-screenshot', dataUrl, filename),
  copyToClipboard: (dataUrl: string) => 
    ipcRenderer.invoke('copy-to-clipboard', dataUrl),
  
  // 监听截图源数据
  onScreenshotSources: (callback: (sources: any[]) => void) => {
    ipcRenderer.on('screenshot-sources', (_event, sources) => callback(sources))
  },
  
  // 移除监听器
  removeScreenshotSourcesListener: () => {
    ipcRenderer.removeAllListeners('screenshot-sources')
  },

  // 监听菜单事件
  onShowOCRStatus: (callback: () => void) => {
    ipcRenderer.on('show-ocr-status', () => callback())
  },
  onInitOCR: (callback: () => void) => {
    ipcRenderer.on('init-ocr', () => callback())
  },

  // 快捷键管理
  checkShortcutConflict: (shortcut: string) => 
    ipcRenderer.invoke('check-shortcut-conflict', shortcut),
  updateShortcut: (name: string, shortcut: string) => 
    ipcRenderer.invoke('update-shortcut', name, shortcut)
})

// 类型声明（仅用于 TypeScript 编译，不会输出到 JS）
// export interface ElectronAPI {
//   takeScreenshot: () => Promise<void>
//   closeScreenshot: () => Promise<void>
//   getScreenSources: () => Promise<Array<{
//     id: string
//     name: string
//     thumbnail: string
//   }>>
//   saveScreenshot: (dataUrl: string, filename: string) => Promise<{
//     success: boolean
//     path?: string
//     error?: string
//   }>
//   autoSaveScreenshot: (dataUrl: string, filename: string) => Promise<{
//     success: boolean
//     path?: string
//     error?: string
//   }>
//   copyToClipboard: (dataUrl: string) => Promise<{
//     success: boolean
//     error?: string
//   }>
//   onScreenshotSources: (callback: (sources: any[]) => void) => void
//   removeScreenshotSourcesListener: () => void
// }

// declare global {
//   interface Window {
//     electronAPI: ElectronAPI
//   }
// }




